package cn.marinda.gui.impl;

import cn.marinda.MinecraftServerHelper;
import cn.marinda.entity.ServerCore;
import cn.marinda.entity.ServerInfo;
import cn.marinda.entity.ServerPlugin;
import cn.marinda.gui.AbstractServerDialog;
import cn.marinda.runnable.ServerImmitConfigRunnable;
import cn.marinda.util.HttpUtils;
import cn.marinda.util.ThreadUtils;
import lombok.Data;

import javax.swing.*;
import java.awt.*;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;

/**
 * 检查版本对话框
 *
 * @author marinda
 * @date 2022/07/14
 */
@Data
public class CheckVersionDialog extends AbstractServerDialog {


    public static CheckVersionDialog instance = new CheckVersionDialog();



    private CheckVersionDialog(){
        super();
    }

    @Override
    protected JDialog loadDialog() {
        dialog = new JDialog();
        dialog.setSize(300,80);
        dialog.setTitle("检查更新");
        dialog.setLocationRelativeTo(null);
        dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        dialog.setResizable(false);
        dialog.setContentPane(view);
        return dialog;
    }

    @Override
    protected JPanel loadView() {
        view  = new JPanel(new FlowLayout(FlowLayout.CENTER));
        Stream<JComponent> componentStream = componentList.stream();
        componentStream.forEach(component -> {
            view.add(component);
        });

        return view;
    }

    @Override
    protected List<JComponent> loadComponent() {
//        最新版本
        String newVersion = HttpUtils.loadVersion().get(0);
        JLabel versionMsg = new JLabel();
//        非最新版本
        if(!MinecraftServerHelper.version.equalsIgnoreCase(newVersion)){
            versionMsg.setText("并非最新版本，请前往下载源更新最新版本！");
        }else{
            versionMsg.setText("已是最新版本！");
        }
        versionMsg.setForeground(Color.RED);

        componentList.add(versionMsg);
        return componentList;
    }


    /**
     * 开放
     */
    @Override
    public void open() {
        dialog.setVisible(true);
    }

    /**
     * 关闭
     */
    @Override
    public void close() {
        dialog.setVisible(false);
    }
}
